﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Business
{
    public static partial class CustomerBLL
    {
        /// <summary>
        /// Xoa khach hang truc tiep
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static int DeletePermanently(long customerID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Customer customer = bitEnt.Customers.SingleOrDefault(ent => ent.CustomerID == customerID);
            if (customer != null)
            {
                NoteGroup note = customer.NoteGroup;
                bitEnt.Customers.DeleteObject(customer);
                //Delete Notegroup
                if (note != null)
                {
                    bitEnt.NoteGroups.DeleteObject(note);
                }
                return bitEnt.SaveChanges();
            }
            return 0;

        }

        /// <summary>
        /// Xoa tam thoi (dat IsDeleted = true)
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static int Delete(long customerID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Customer c = bitEnt.Customers.SingleOrDefault(ent => ent.CustomerID == customerID);
            c.IsDeleted = true;
            return bitEnt.SaveChanges();
        }

        /// <summary>
        /// Them khach hang
        /// </summary>
        /// <param name="Customer"></param>
        /// <returns></returns>
        public static int Insert(this Customer customer)
        {            
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            //Add NoteGroup
            NoteGroup notegroup = new NoteGroup() { LastNoteDate = DateTime.Now };
            bitEnt.NoteGroups.AddObject(notegroup);
            //Add Customer
            customer.NoteGroup = notegroup;
            bitEnt.Customers.AddObject(customer);
            return bitEnt.SaveChanges();
        }

        /// <summary>
        /// Tra ve tat ca cac khach hang
        /// </summary>
        /// <returns></returns>
        public static IQueryable<Customer> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ent in bitEnt.Customers
                       where ent.IsDeleted == false
                       select ent;
            return list;
        }

        /// <summary>
        /// Lay ra khach hang theo ID truyen vao
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static Customer GetByID(long customerID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Customers.SingleOrDefault(ent => ent.CustomerID == customerID);
        }


    }
}